package com.fzrskj.cloud.free.dic.utils;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.dic.dao.DicParamItemDao;
import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DicParamItemUtils extends CObject{
	/**
	 * 获取索引值列表
	 * @param paramId
	 * @param itemIdList
	 * @return
	 */
	public List<DicParamItem> getDicParamItemList(String paramId, List<Integer> itemIdList) {
		DicParamItemDao dao = new DicParamItemDao();
		List<DicParamItem> list = dao.getDicParamItemList(paramId, itemIdList);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 根据数据值查询数据字典
	 * @param value
	 * @return
	 */
	public DicParamItem getSysParamItemByValue(String value) {
		DicParamItemDao dao = new DicParamItemDao();
		DicParamItem sys = dao.getSysParamItemByValue(value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}

	/**
	 * 获取字典描述.
	 * @param value
	 * @return
	 */
	public static String getDescriptionByValue(String value) {
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		DicParamItem dicParamItem = dicParamItemUtils.getSysParamItemByValue(value);
		return dicParamItem == null ? null : dicParamItem.getDescription();
 	}

	/**
	 * 获取字典描述.
	 * @param value
	 * @return
	 */
	public static String getDescriptionByValue(String value,String paramId) {
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		DicParamItem dicParamItem = dicParamItemUtils.getSysParamItemByValue(value, paramId);
		return dicParamItem == null ? null : dicParamItem.getDescription();
	}

	/**
	 * 通过数据值查询字段
	 * @param value
	 * @param paramId
	 * @return
	 */
	public DicParamItem getSysParamItemByValue(String value,String paramId) {
		DicParamItemDao dao = new DicParamItemDao();
		DicParamItem sys = dao.getSysParamItemByValue(value, paramId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}

	public List<DicParamItem> getListSysParamItemByEName(String paramEName,int paramType) {
		return this.getListSysParamItemByEName(paramEName, paramType, "");
	}

	/**
	 * 通过索引值获取具体配置列表
	 * @param paramEName
	 * @param paramType
	 * @return
	 */
	public List<DicParamItem> getListSysParamItemByEName(String paramEName,int paramType, String value) {
		DicParamItemDao dao = new DicParamItemDao();
		List<DicParamItem> list = dao.getListSysParamItemByEName(paramEName, paramType, value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 通过类别获取子列表
	 * @param paramType
	 * @return
	 */
	public List<DicParamItem> getListSysParamItemByType(int paramType) {
		DicParamItemDao dao = new DicParamItemDao();
		List<DicParamItem> list = dao.getListSysParamItemByType(paramType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 通过Dic_paramIndex的paramEName和Dic_paramItem的value获取对象
	 * @param paramEName
	 * @param value
	 * @param paramType
	 * @return
	 */
	public DicParamItem getParamItemByENameAndValue(String paramEName,String value, int paramType) {
		DicParamItemDao dao = new DicParamItemDao();
		DicParamItem sys = dao.getParamItemByENameAndValue(paramEName, value, paramType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}

	/**
	 * 根据条件修改value
	 * @param id
	 * @param description
	 * @param remark
	 * @return
	 */
	public int updateParamitem(int id,String description,String remark){
		DicParamItemDao dao = new DicParamItemDao();
		int i = dao.updateParamitem(id, description,remark);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 添加数据子字典
	 * @param obj
	 * @return
	 */
	public int addSysParamitem(DicParamItem obj){
		int i = 0;
		if((getSysParamItemByValue(obj.getValue(), obj.getParamId())) !=null){
			this.setErrCode(AppCode.DIC_ITEM_EXISTS);
			return i;
		}

		DicParamItemDao dao = new DicParamItemDao();
		i = dao.addSysParamitem(obj);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 添加数据子字典
	 * @param values
	 * @return
	 */
	public int addSysParamitem(Map<String, Object> values){
		DicParamItemDao dao = new DicParamItemDao();
		int i = addSysParamitem(values);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 删除数据字典子列表
	 * @param id
	 * @param paramId
	 * @param value
	 * @return
	 */
	public int delSysParamitem(int id,String paramId,String value){
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("id", id);
		cdt.put("paramId", paramId);
		int ret=delSysParamitem(cdt);

		recursionDelItem(value);
		return ret;
	}

	//递归删除项
	private int recursionDelItem(String value){
		int ret=0;
		List<DicParamItem> list=getDicParamItemList(value,null);
		if(list.size()>0){
			for(DicParamItem dpi:list){
				Map<String, Object> cdt = new HashMap<String, Object>();
				cdt.put("id", dpi.getId());
				cdt.put("paramId", dpi.getParamId());
				delSysParamitem(cdt);
				
				List<DicParamItem> subList=getDicParamItemList(dpi.getValue(),null);
				if(subList.size()>0)
					recursionDelItem(dpi.getValue());
			}
		}
		return ret;
	}
	
	public int delSysParamitem(String paramId){
		DicParamItemDao dao = new DicParamItemDao();
		int i = dao.delSysParamitem(paramId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	public int delSysParamitem(Map<String, Object> cdt){
		DicParamItemDao dao = new DicParamItemDao();
		int i = dao.delSysParamitem(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	public int modSysParamitem(Map<String, Object> values, Map<String, Object> cdt){
		DicParamItemDao dao = new DicParamItemDao();
		int i = dao.modSysParamitem(values, cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	public DicParamItem querySysParamitem(int id){
		DicParamItemDao dao = new DicParamItemDao();
		DicParamItem sys = dao.querySysParamitem(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sys;
	}

	public DicParamItem querySysParamitemByRule(Map<String, Object> cdt){
		List<DicParamItem> dicParamItemList = this.querySysParamitemList(cdt, 1, 1);
		return dicParamItemList.isEmpty() ? null : dicParamItemList.get(0);
	}
	
	public int querySysParamitem(Map<String, Object> cdt){
		DicParamItemDao dao = new DicParamItemDao();
		int i = dao.querySysParamitem(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}
	
	public List<DicParamItem> querySysParamitemList(Map<String, Object> cdt, int page, int pagesize){
		DicParamItemDao dao = new DicParamItemDao();
		List<DicParamItem> list = dao.querySysParamitemList(cdt, page, pagesize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	//在指定父级下，通过Description查询内容
	public static DicParamItem querySysParamitemByParamIdAndDesc(String paramId, String desc){
		DicParamItem ret = null;
		DicParamItemUtils utils = new DicParamItemUtils();
		List<DicParamItem> list = utils.getDicParamItemList(paramId,null);
		if(list.size()>0){
			for(DicParamItem dpi:list){
				if (desc.equals(dpi.getDescription())){
					ret = dpi;
					break;
				}
			}
		}
		return ret;
	}

	public List<DicParamItem> getVHostSupportWebSoft(String[] webSofts) {
		List<DicParamItem> newList = new ArrayList<>();
		String paramEName = "controlSoft";
		String value = "";
		DicParamItemDao dao = new DicParamItemDao();
		List<DicParamItem> list = dao.getListSysParamItemByEName(paramEName, 0, value);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		for (DicParamItem d : list) {
			for (String s : webSofts) {
				if (d.getValue().equals(s)) {
					newList.add(d);
				}
			}
		}
		return newList;
	}
}